package com.github.ghsea.cc.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

import org.apache.storm.shade.org.apache.commons.lang.StringUtils;
import org.apache.storm.shade.org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.ghsea.cc.module.NginxAccessLog;
import com.google.common.base.Splitter;

public class AccessLogParser {

	private static Logger logger = LoggerFactory.getLogger(AccessLogParser.class);

	private static Splitter sp = Splitter.on(Pattern.compile("\"\\s*")).omitEmptyStrings();

	// 时间格式"28/Jul/2017:20:58:54 +0800"
	private static String dateFormat = "dd/MMM/yyyy:HH:mm:ss Z";

	private AccessLogParser() {

	}

	public static NginxAccessLog parse(String accessLog) {
		List<String> filedList = sp.splitToList(accessLog);
		NginxAccessLog logVo = new NginxAccessLog();
		logVo.setAccessIp(filedList.get(0));

		Date accessTime = parseTime(filedList.get(2));
		logVo.setAccessTime(accessTime);
		logVo.setRequestHost(StringUtils.trim(filedList.get(3)));

		// "GET /index.jsp HTTP/1.1"
		String requestUrl = StringUtils.split(filedList.get(4), " ")[1].trim();
		logVo.setRequestUrl(requestUrl);
		String responseCode = StringUtils.split(filedList.get(5), " ")[0].trim();
		logVo.setResponseCode(NumberUtils.toInt(responseCode));
		logVo.setReferer(filedList.get(6));
		logVo.setUserAgent(filedList.get(7));
		logVo.setCookie(filedList.get(9));

		return logVo;
	}
	
	private static Date parseTime(String timeStr) {
		SimpleDateFormat s = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
		Date time = null;
		try {
			time = s.parse(timeStr);
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
		}
		return time;
	}

	public static void main(String[] args) throws ParseException {
		String s = "\"192.168.1.101\" \"-\" \"28/Jul/2017:20:58:54 +0800\" \"GET /index.jsp HTTP/1.1\" 404 571 \"-\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36\" \"-\" \"-\"";
		System.out.println(parse(s));
	}
}
